package org.jackysoft.service.impl;

import java.util.Collection;

import javax.annotation.Resource;

import org.jackysoft.entity.FileItem;
import org.jackysoft.file.FileType;
import org.jackysoft.file.TranslateJDBCWorker;
import org.jackysoft.service.AbstractStringIdServiceImpl;
import org.jackysoft.service.TranslateService;
import org.jackysoft.util.CollectionUtils;
import org.jackysoft.util.SpringSecurityUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TranslateServiceImpl extends AbstractStringIdServiceImpl<FileItem>
		implements TranslateService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5309878549085862992L;

	private TranslateJDBCWorker jdbcWork;
	

	@Override
	public void translate(String fileType, 
			Collection<String> fileIds,
			Collection<String> toUsers) throws DataAccessException {
	   String fromer = SpringSecurityUtils.getCurrentUserName();
	   CollectionUtils.notNullable(toUsers);
	   CollectionUtils.notNullable(fileIds);
	   
	   jdbcWork.setFileIds(fileIds);
	   jdbcWork.setFromer(fromer);
	   jdbcWork.setToUsers(toUsers);
	   jdbcWork.setType(FileType.getFileType(fileType));
	   jdbcWork.setSessionFactory(sessionFactory);
	   getSession().doWork(jdbcWork);
	   
	}


	@Override
	public void changeDir(Collection<String> fileIds, String folder)
			throws DataAccessException {
		String hql = "update UploadedFileItem set folder='"+folder+"',createTime=now()  where id in (:ids)";
        getSession()
        .createQuery(hql)
        .setParameterList("ids", fileIds)
        .executeUpdate();
        hql = "update ReceivedFileItem set folder='"+folder+"',createTime=now()  where id in (:ids)";
        getSession()
        .createQuery(hql)
        .setParameterList("ids", fileIds)
        .executeUpdate();
	
	}


	@Resource(shareable=false)
	public void setJdbcWork(TranslateJDBCWorker jdbcWork) {
		this.jdbcWork = jdbcWork;
	}


	public TranslateJDBCWorker getJdbcWork() {
		return jdbcWork;
	}

	

}
